package com.joe.updemo;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import com.joe.updemo.http.ApiHelper;
import com.joe.updemo.http.listener.TransApiResultListener;
import com.joe.updemo.utils.ProgressDialogUtil;
import com.joe.updemo.utils.manager.ToastUtil;

import java.math.RoundingMode;
import java.text.DecimalFormat;

import static com.joe.updemo.utils.PayContext.DEMO_ORDER_MONEY_DEF;

public class MainActivity extends Activity {

    private WebView mWebView;
    private EditText editText;
    private ProgressDialogUtil mLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initSettings();
    }

    private void initView() {
        mWebView = findViewById(R.id.webView);
        editText = findViewById(R.id.et_order_money);
    }

    public void test(View view) {
        mWebView.loadUrl("");
        String orderMoney;
        if (TextUtils.isEmpty(editText.getText().toString())) {
            orderMoney = DEMO_ORDER_MONEY_DEF;
        } else {
            orderMoney = editText.getText().toString();
        }
        orderMoney = formatMoney(orderMoney);
        editText.setText(orderMoney);
        forceHideKeyBoard();
        ApiHelper.getInstance(this).OrderAPI(orderMoney, new TransApiResultListener<String>() {
            @Override
            public void trans(final String msg) {
                mWebView.loadUrl(msg);
            }
        });
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initSettings() {
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBlockNetworkImage(false);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        SimpleWebViewClient simpleWebViewClient = new SimpleWebViewClient();
        mWebView.setWebViewClient(simpleWebViewClient);
    }

    private class SimpleWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            showLoading();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            dismissLoading();
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
    }

    private void showLoading() {
        if (null == mLoading) {
            mLoading = new ProgressDialogUtil();
        }
        if (!mLoading.isShowing()) {
            mLoading.showProgress(this, getResources().getString(R.string.loading));
        }
    }

    private void dismissLoading() {
        if (null != mLoading) {
            mLoading.dismissProgress();
        }
    }

    DecimalFormat decimalFormat = new DecimalFormat("0.00");

    private String formatMoney(String money) {
        try {
            decimalFormat.setRoundingMode(RoundingMode.DOWN);
            money = decimalFormat.format(Double.parseDouble(money));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            ToastUtil.showShort("数据错误,改为默认值");
            return DEMO_ORDER_MONEY_DEF;
        }
        return money;
    }

    private void forceHideKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) {
            return;
        }
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //强制隐藏键盘
    }
}